using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Medianamik.Globalization;
using Medianamik.Globalization.Utilities;
using Medianamik.UI.Web;
using NUnit.Framework;

namespace Medianamik.Tests.Unit.Globalization.Utilities
{
    public abstract class ResourceExporterTest<T> where T : class
    {
        protected abstract ResourceExporter<T> GetExporter();
        protected abstract int ResourceCount { get; }

        protected List<Resource> _resources;
        protected IMedianamikResourceDataManager _manager;
        protected string _firstResourceSetId;

        [Test]
        public void Exporter_should_throw_NullArgumentException_when_manager_is_null_OR_resourceSets_are_null()
        {
            var exporter = GetExporter();
            
            Assert.Throws<ArgumentNullException>(() => exporter.Export(new[] { "" }, null));
            Assert.Throws<ArgumentNullException>(() => exporter.Export(null, _manager));
        }

        [Test]
        public void Exporter_should_throw_ArgumentException_when_resourceSets_is_empty()
        {
            var exporter = GetExporter();
            Assert.Throws<ArgumentException>(() => exporter.Export(new string[] { }, _manager));
        }
        
        [Test]
        public void ResourceCount_should_be_equal_to_every_distinct_resourceId_times_every_languages_when_they_are_specified()
        {
            foreach (var resource in _resources)
                _manager.UpdateOrAdd(resource);

            var exporter = GetExporter();
            var langs = new List<string>();

            foreach(var res in _resources)
            {
                if (_firstResourceSetId == res.ResourceSet && !langs.Contains(res.CultureName))
                    langs.Add(res.CultureName);
            }
            var resourceIds = new List<string>();
            _resources.ForEach(x =>
                                   {
                                       if (_firstResourceSetId == x.ResourceSet && !resourceIds.Contains(x.Id))
                                           resourceIds.Add(x.Id);
                                   });
            
            exporter.LanguagesToExport = langs;
            var expectedCount = resourceIds.Count*langs.Count;
            var actualCount = ResourceCount;

            Assert.That(actualCount, Is.EqualTo(expectedCount));
        }

        protected void AddResources()
        {
            _resources = new List<Resource>
                             {
                                 new Resource("0", "fr-CA", "allo", "a"),
                                 new Resource("1", "fr-CA", "au revoir", "a"),
                                 new Resource("1", "en-CA", "bye", "a"),
                                 new Resource("2", "sp-SP", "hola senorita", "a"),
                             };
        }
    }
}